# -*- coding: UTF-8 -*-
from typing import List
from leetcode_helper import TestcaseHelper, SolutionBase


class Solution(SolutionBase):
    # 解题的方法，命名和Leetcode的保持一致即可
    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
        class Rect(object):
            def __init__(self, x1, y1, x2, y2):
                self.x1 = x1
                self.y1 = y1
                self.x2 = x2
                self.y2 = y2
                self.pts = []
                self.pts.append([x1, y2])
                self.pts.append([x1, y1])
                self.pts.append([x2, y2])
                self.pts.append([x2, y1])

            def points(self):
                return self.pts

            def is_cross(self, rect) -> bool:
                for item in rect.pts:
                    if self.x1 <= item[0] <= self.x2 and self.y1 <= item[1] <= self.y2:
                        return True

                if self.x1 <= rect.x1 < rect.x2 <= self.x2:
                    if rect.y1 <= self.y1 < self.y2 <= rect.y2:
                        return True

                return False

            def area(self) -> int:
                return abs(self.x1 - self.x2) * abs(self.y1 - self.y2)

            def cross_area(self, rect) -> int:
                x_axis = [self.x1, self.x2, rect.x1, rect.x2]
                y_axis = [self.y1, self.y2, rect.y1, rect.y2]
                x_axis.sort()
                y_axis.sort()
                return abs(x_axis[1] - x_axis[2]) * abs(y_axis[1] - y_axis[2])

        rect1 = Rect(ax1, ay1, ax2, ay2)
        rect2 = Rect(bx1, by1, bx2, by2)

        total = rect1.area() + rect2.area()
        dup_area = 0

        if rect1.is_cross(rect2):
            dup_area = rect1.cross_area(rect2)
        elif rect2.is_cross(rect1):
            dup_area = rect2.cross_area(rect1)

        return total - dup_area


if __name__ == '__main__':
    solution = Solution()

    # 按照leetcode的题目说明，将基本的test case填入
    # 每条case的最后一个元素是正确的结果，即期待的结果
    # 使用元组来保存输入的数据，期待的结果
    testcases = [
        (-5, -2, 5, 1, -3, -3, 3, 3, 48),
        (-3, 0, 3, 4, 0, -1, 9, 2, 45),
        (-2, -2, 2, 2, -2, -2, 2, 2, 16)
    ]

    for case_item in testcases:
        # 获取测试用的case和期待的执行结果
        input1 = case_item[0]
        input2 = case_item[1]
        input3 = case_item[2]
        input4 = case_item[3]
        input5 = case_item[4]
        input6 = case_item[5]
        input7 = case_item[6]
        input8 = case_item[7]

        expect = case_item[len(case_item) - 1]

        exec_result = solution.computeArea(input1, input2,
                                           input3, input4,
                                           input5, input6,
                                           input7, input8)

        # 判断执行结果，输出提示信息
        check_result = solution.check_result(expect, exec_result)
        TestcaseHelper.print_case(check_result, case_item, exec_result)

